package  
{
	import away3d.containers.View3D;
	import away3d.lights.DirectionalLight;
	import away3d.lights.LightBase;
	import away3d.lights.PointLight;
	import away3d.materials.ColorMaterial;
	import away3d.materials.lightpickers.LightPickerBase;
	import away3d.materials.lightpickers.StaticLightPicker;
	import away3d.materials.MaterialBase;
	import away3d.materials.methods.BasicDiffuseMethod;
	import away3d.materials.methods.CascadeShadowMapMethod;
	import away3d.materials.TextureMaterial;
	import away3d.utils.Cast;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Vector3D;
	import org.flashdevelop.utils.FlashConnect;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.WireframeMaterial;	

	
	public class MaterialLib
	{
		public static var SIMPLE_TILE:TextureMaterial;
		public static var SIMPLE_TILE2:TextureMaterial;
		public static var SIMPLE_TILE3:TextureMaterial;
		public static var SIMPLE_TILE4:TextureMaterial;
		public static var SIMPLE_TILE5:TextureMaterial;
		
		public static var CRUMBLING_TILE:TextureMaterial;
		public static var CRUMBLING_TILE_ALPHA:TextureMaterial;
		
		public static var MASTER_CUBE:TextureMaterial;
		public static var MASTER_GOAL:TextureMaterial;
		
		public static var SLAVE_CUBE_A:TextureMaterial;		
		public static var SLAVE_LINK_A:TextureMaterial;		
		public static var SLAVE_GOAL_A:TextureMaterial;
		
		public static var SLAVE_CUBE_B:TextureMaterial;		
		public static var SLAVE_LINK_B:TextureMaterial;		
		public static var SLAVE_GOAL_B:TextureMaterial;		
		
		public static var NEGATOR_TILE:ColorMaterial;
		public static var ROTATE_CLOCKWISE_MOD:TextureMaterial;
		public static var BOOST_MOD:TextureMaterial;
		public static var GRID_MATERIAL:MaterialBase;
		public static var BLOCKER_CUBE:MaterialBase;
		public static var TELEPORT_TILE:TextureMaterial;
		
		public static var MIRROR_Z:TextureMaterial;
		
		public static var ACTIVATED:TextureMaterial;
		public static var UNACTIVATED:TextureMaterial;		
		
		public static var ACTIVATOR_OFF:TextureMaterial;
		public static var ACTIVATOR_ON:TextureMaterial;
		
		public static var REPULSOR:ColorMaterial;
		
		public static var PARTICLE_MATERIAL:TextureMaterial;
		
		
		
		
		public static var GLOBAL_LIGHT:PointLight;
		public static var SLAVE_LIGHT_A:PointLight;
		public static var SLAVE_LIGHT_B:PointLight;
		public static var MASTER_LIGHT:PointLight;
		
		public static var SIMPLE_TILES:Vector.<TextureMaterial>;


		[Embed(source = "../img/basic_tile2.jpg")]
		public static var FloorDiffuse:Class;
		
		[Embed(source = "../img/rot3.jpg")]
		public static var RotationIcon:Class;		
		
		[Embed(source = "../img/multi.jpg")]
		public static var MultiIcon:Class;	
		
		[Embed(source = "../img/mirror_z.jpg")]
		public static var MirrorIcon:Class;			
		
		[Embed(source = "../img/master.jpg")]
		public static var MasterTexture:Class;			

		[Embed(source = "../img/link_a.jpg")]
		public static var LinkAIcon:Class;	
		
		[Embed(source = "../img/link_b.jpg")]
		public static var LinkBIcon:Class;			
		
		[Embed(source = "../img/crumbling.jpg")]
		public static var CrumblingTile:Class;				
		
		[Embed(source = "../img/slave_a.jpg")]
		public static var SlaveATexture:Class;	
		
		[Embed(source = "../img/slave_b.jpg")]
		public static var SlaveBTexture:Class;			
		
		[Embed(source = "../img/teleport.jpg")]
		public static var TeleportTexture:Class;
		
		[Embed(source = "../img/activator_off.jpg")]
		public static var ActivatorOff:Class;
		
		[Embed(source = "../img/activator_on.jpg")]
		public static var ActivatorOn:Class;
		
		[Embed(source = "../img/activated.jpg")]
		public static var Activated:Class;
		
		[Embed(source = "../img/particle.png")]
		public static var ParticleTexture:Class;
		
				
		public static var frame_listener:Sprite = new Sprite();
		public static var teleport_anim_time:int = 0;
		
		public function MaterialLib() 
		{
			
		}
		
		
		public static function init():void
		{
			MASTER_LIGHT = new PointLight();			
			MASTER_LIGHT.color = 0xff0000;
			MASTER_LIGHT.ambient = 0;
			MASTER_LIGHT.diffuse = .5;
			MASTER_LIGHT.specular = 1;	
			MASTER_LIGHT.fallOff = 100;
			MASTER_LIGHT.radius = 25;
			
			SLAVE_LIGHT_A = new PointLight();			
			SLAVE_LIGHT_A.color = 0x0000ff;
			SLAVE_LIGHT_A.ambient = 0;
			SLAVE_LIGHT_A.diffuse = 1;
			SLAVE_LIGHT_A.specular = 2;			
			SLAVE_LIGHT_A.fallOff = 100;
			SLAVE_LIGHT_A.radius = 25;
			
			SLAVE_LIGHT_B = new PointLight();
			SLAVE_LIGHT_B.color = 0x00ff00;
			SLAVE_LIGHT_B.ambient = 0;
			SLAVE_LIGHT_B.diffuse = .3;
			SLAVE_LIGHT_B.specular = .7;
			SLAVE_LIGHT_B.fallOff = 100;
			SLAVE_LIGHT_B.radius = 25;
			
			GLOBAL_LIGHT = new PointLight();
			GLOBAL_LIGHT.color = 0xffffff;
			GLOBAL_LIGHT.ambient = .2;
			GLOBAL_LIGHT.diffuse = .8;
			GLOBAL_LIGHT.specular = .2;
			GLOBAL_LIGHT.y = 250;
			
			var light_picker:StaticLightPicker = new StaticLightPicker([MASTER_LIGHT, SLAVE_LIGHT_A, SLAVE_LIGHT_B, GLOBAL_LIGHT]);			
			//var light_picker:StaticLightPicker = new StaticLightPicker([GLOBAL_LIGHT]);			
			//var light_picker:StaticLightPicker = new StaticLightPicker([]);			
						
			SIMPLE_TILE = new TextureMaterial(Cast.bitmapTexture(FloorDiffuse));									
			SIMPLE_TILE.gloss = 10;
			SIMPLE_TILE.ambient = .7;			
			SIMPLE_TILE.specular = .3;
			SIMPLE_TILE.lightPicker = light_picker;			

			SIMPLE_TILE2 = new TextureMaterial(Cast.bitmapTexture(FloorDiffuse));									
			SIMPLE_TILE2.gloss = 10;
			SIMPLE_TILE2.ambient = .8;			
			SIMPLE_TILE2.specular = .4;
			SIMPLE_TILE2.lightPicker = light_picker;	
			
			SIMPLE_TILE3 = new TextureMaterial(Cast.bitmapTexture(FloorDiffuse));									
			SIMPLE_TILE3.gloss = 10;
			SIMPLE_TILE3.ambient = .6;			
			SIMPLE_TILE3.specular = .2;
			SIMPLE_TILE3.lightPicker = light_picker;	
			
			SIMPLE_TILE4 = new TextureMaterial(Cast.bitmapTexture(FloorDiffuse));									
			SIMPLE_TILE4.gloss = 10;
			SIMPLE_TILE4.ambient = .8;			
			SIMPLE_TILE4.specular = .2;
			SIMPLE_TILE4.lightPicker = light_picker;	
			
			SIMPLE_TILE5 = new TextureMaterial(Cast.bitmapTexture(FloorDiffuse));									
			SIMPLE_TILE5.gloss = 10;
			SIMPLE_TILE5.ambient = .6;			
			SIMPLE_TILE5.specular = .3;
			SIMPLE_TILE5.lightPicker = light_picker;
			
			SIMPLE_TILES = new <TextureMaterial> [SIMPLE_TILE, SIMPLE_TILE2, SIMPLE_TILE3, SIMPLE_TILE4, SIMPLE_TILE5];						
			
			MASTER_CUBE = new TextureMaterial(Cast.bitmapTexture(MasterTexture));					
						
			MASTER_GOAL = new TextureMaterial(Cast.bitmapTexture(MasterTexture));
			MASTER_GOAL.lightPicker = light_picker;
			
			
			
			SLAVE_CUBE_A = new TextureMaterial(Cast.bitmapTexture(SlaveATexture));//new ColorMaterial(0x4040dd, .9);			
			
			SLAVE_GOAL_A = new TextureMaterial(Cast.bitmapTexture(SlaveATexture));//new ColorMaterial(0x4040dd, .9);
			SLAVE_GOAL_A.lightPicker = light_picker;
						
			SLAVE_LINK_A = new TextureMaterial(Cast.bitmapTexture(LinkAIcon));//new ColorMaterial(0x4040dd, .9);			
			SLAVE_LINK_A.specular = .1;
			SLAVE_LINK_A.lightPicker = light_picker;
			

			SLAVE_CUBE_B = new TextureMaterial(Cast.bitmapTexture(SlaveBTexture));//new ColorMaterial(0x4040dd, .9);			
			
			SLAVE_GOAL_B = new TextureMaterial(Cast.bitmapTexture(SlaveBTexture));//new ColorMaterial(0x4040dd, .9);
			SLAVE_GOAL_B.lightPicker = light_picker;
						
			SLAVE_LINK_B = new TextureMaterial(Cast.bitmapTexture(LinkBIcon));//new ColorMaterial(0x4040dd, .9);			
			SLAVE_LINK_B.specular = .1;
			SLAVE_LINK_B.lightPicker = light_picker;			
			
			CRUMBLING_TILE = new TextureMaterial(Cast.bitmapTexture(CrumblingTile));//new ColorMaterial(0x77bbbb, .5);
			CRUMBLING_TILE.specular = 0.2;
			CRUMBLING_TILE.alpha = .9;
			CRUMBLING_TILE.lightPicker = light_picker;
			
			
			CRUMBLING_TILE_ALPHA = new TextureMaterial(Cast.bitmapTexture(CrumblingTile));			
			CRUMBLING_TILE_ALPHA.specular = 0.2;
			CRUMBLING_TILE_ALPHA.alpha = .9;
			CRUMBLING_TILE_ALPHA.lightPicker = light_picker;			
			
			
			NEGATOR_TILE = new ColorMaterial(0x666666, 1);
			NEGATOR_TILE.ambient = .1;
			NEGATOR_TILE.specular = .4;
			NEGATOR_TILE.alpha = 1;
			NEGATOR_TILE.lightPicker = light_picker;
						
			
			ROTATE_CLOCKWISE_MOD = new TextureMaterial(Cast.bitmapTexture(RotationIcon));//new ColorMaterial(0xcc77ee, .95);
			ROTATE_CLOCKWISE_MOD.lightPicker = light_picker;
			
			
			BOOST_MOD = new TextureMaterial(Cast.bitmapTexture(MultiIcon));//new ColorMaterial(0x7744bb, .95);			
			BOOST_MOD.gloss = 10;
			BOOST_MOD.ambient = .8;		
			BOOST_MOD.specular = 1;						
			BOOST_MOD.lightPicker = light_picker;
			
			
			GRID_MATERIAL = new ColorMaterial(0xff0000, .00000001);
			
			BLOCKER_CUBE = new TextureMaterial(Cast.bitmapTexture(FloorDiffuse));
			BLOCKER_CUBE.lightPicker = light_picker;
			
			//CRUMBLING_TILE = new ColorMaterial(0x77bbbb, .95);
			
			
			ACTIVATOR_OFF = new TextureMaterial(Cast.bitmapTexture(ActivatorOff));//new ColorMaterial(0xcc5555, 1);	
			ACTIVATOR_OFF.gloss = SIMPLE_TILE.gloss;
			ACTIVATOR_OFF.ambient = SIMPLE_TILE.ambient;			
			ACTIVATOR_OFF.specular = SIMPLE_TILE.specular;			
			ACTIVATOR_OFF.lightPicker = light_picker;
			
			ACTIVATOR_ON = new TextureMaterial(Cast.bitmapTexture(ActivatorOn));//new ColorMaterial(0x55cc55, 1);
			ACTIVATOR_ON.gloss = SIMPLE_TILE.gloss;
			ACTIVATOR_ON.ambient = SIMPLE_TILE.ambient;
			ACTIVATOR_ON.specular = SIMPLE_TILE.specular;
			ACTIVATOR_ON.lightPicker = light_picker;
			
			ACTIVATED = new TextureMaterial(Cast.bitmapTexture(Activated));//new ColorMaterial(0xcc5555, 1);	
			ACTIVATED.gloss = SIMPLE_TILE.gloss;
			ACTIVATED.ambient = .8;			
			ACTIVATED.specular = SIMPLE_TILE.specular;			
			ACTIVATED.alpha = 1;
			ACTIVATED.lightPicker = light_picker;
			
			UNACTIVATED = new TextureMaterial(Cast.bitmapTexture(Activated));//new ColorMaterial(0xcc5555, 1);	
			UNACTIVATED.gloss = SIMPLE_TILE.gloss;
			UNACTIVATED.ambient = SIMPLE_TILE.ambient;			
			UNACTIVATED.specular = SIMPLE_TILE.specular;	
			UNACTIVATED.alpha = .3;
			UNACTIVATED.lightPicker = light_picker;//new ColorMaterial(0x66ff66, .1);
			
			TELEPORT_TILE = new TextureMaterial(Cast.bitmapTexture(TeleportTexture));//new ColorMaterial(0xffcccc, .95);
			TELEPORT_TILE.lightPicker = light_picker;			
			
			MIRROR_Z = new TextureMaterial(Cast.bitmapTexture(MirrorIcon));//new ColorMaterial(0x66cccc, .95);
			MIRROR_Z.lightPicker = light_picker;
			
			REPULSOR = new ColorMaterial(0x666699, 1);
			REPULSOR.lightPicker = light_picker;
			
			PARTICLE_MATERIAL = new TextureMaterial(Cast.bitmapTexture(ParticleTexture));
			PARTICLE_MATERIAL.alphaBlending = true;
			
			
			frame_listener.addEventListener(Event.ENTER_FRAME, animate_tex_hnd);
		}
		
		
		public static function animate_tex_hnd(e:Event):void
		{
			teleport_anim_time++;
			
			TELEPORT_TILE.ambient = 1 + Math.sin(teleport_anim_time / 30) / 2;
			MIRROR_Z.ambient = .8 + Math.sin(teleport_anim_time / 35 + 12) / 3;		
			
			SLAVE_LINK_A.ambient = 1 + Math.sin(teleport_anim_time / 15 + 12) / 4;
			SLAVE_LINK_B.ambient = 1 + Math.sin(teleport_anim_time / 22 + 12) / 4;
			
			MASTER_GOAL.ambient = 1 + Math.sin(teleport_anim_time / 45 + 12) / 4;			
			SLAVE_GOAL_A.ambient = 1 + Math.sin(teleport_anim_time / 40 + 5) / 3;			
			SLAVE_GOAL_B.ambient = 1 + Math.sin(teleport_anim_time / 35 + 15) / 3;			
		}
		
		
		
	}

}